//
//  TWWelcomeViewController.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/3/14.
//  Copyright © 2017年 个人. All rights reserved.
//

import UIKit
import SDWebImage


class TWWelcomeViewController: UIViewController {

    @IBOutlet weak var BigImageView: UIImageView!
    @IBOutlet weak var logoView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        requsetImage()
    }
    
    private func requsetImage(){
        /// 显示大图背景
        TWNetworkTool.shareInstance.request(methodType: .get, URLString: WelcomeUrlString, parameters: [:]) { (result, error) in
            if error != nil {
                TWLog(message: error)
                // 失败了不能卡这，直接进入主页
                UIApplication.shared.keyWindow?.rootViewController = TWTabBarController()
                return
            }
            guard let result = result else {
                return
            }
            let startPage = result["startPage"] as! [String : AnyObject]
            let imageUrl = startPage["imageUrl"] as! String
            let imageStr = URL(string: imageUrl)
            // 淡入
            UIView.transition(with: self.BigImageView, duration: 0.5, options: UIViewAnimationOptions.transitionCrossDissolve, animations: {
                self.BigImageView.sd_setImage(with: imageStr, completed: { (_, error, _, _) in
                    if error == nil {
                        // 下载完成之后开始动画
                        UIView.animate(withDuration: 3.0, animations: {
                            self.BigImageView.alpha = 0.8
                            self.logoView.alpha = 1.0
                            self.BigImageView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
                        }, completion: { (_) in
                            UIApplication.shared.keyWindow?.rootViewController = TWTabBarController()
                        })
                    }
                })
            }, completion: nil)
        }
    }
}
